import { Controller, Get, Param } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

@Controller('qq/zljbz')
export class ZljbzController {
    constructor(
        private readonly prismaService: PrismaService
    ) { }

    @Get(":tid/get")
    async get(
        @Param("tid") tid: string
    ) {
        const res = await this.prismaService.zljbz.findFirst({
            where: { tid },
            include: {
                zljbzCheck: true
            }
        })
        if (!res) return [];
        return (res.zljbzCheck || []).map(e => e.ans)
    }

    @Get(":tid/:ans/set")
    async set(
        @Param("tid") tid: string,
        @Param("ans") ans: any
    ) {
        ans = parseInt(ans);
        const data = await this.prismaService.zljbz.findFirst({ where: { tid } });
        if (!data) {
            await this.prismaService.zljbz.create({
                data: {
                    tid,
                    zljbzCheck: {
                        create: {
                            ans
                        }
                    }
                }
            })
            return;
        }
        await this.prismaService.zljbzCheck.create({
            data: {
                ans,
                target: {
                    connect: {
                        id: data.id
                    }
                }
            }
        })
    }
}
